package com.ruoyi.cloudcourse.controller;

import java.util.List;

import com.ruoyi.cloudcourse.domain.FeatherSpecial;
import com.ruoyi.cloudcourse.service.IFeatherSpecialService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.cloudcourse.domain.FeatherCourse;
import com.ruoyi.cloudcourse.service.IFeatherCourseService;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 课程Controller
 * 
 * @author ruoyi
 * @date 2021-07-13
 */
@Controller
@RequestMapping("/cloudcourse/course")
public class FeatherCourseController extends BaseController
{
    private String prefix = "cloudcourse/course";

    @Autowired
    private IFeatherCourseService featherCourseService;
    @Autowired
    private IFeatherSpecialService featherSpecialService;

    @RequiresPermissions("cloudcourse:course:view")
    @GetMapping()
    public String course(Model model)
    {
        FeatherSpecial special = new FeatherSpecial();
        List<FeatherSpecial> specialList = featherSpecialService.selectFeatherSpecialList(special);
        model.addAttribute("specialList",specialList);
        return prefix + "/course";
    }

    /**
     * 查询课程列表
     */
    @RequiresPermissions("cloudcourse:course:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(FeatherCourse featherCourse,String specialId)
    {

        startPage();
        List<FeatherCourse> list = featherCourseService.selectFeatherCourseList(featherCourse);
        if ( specialId == null || specialId.trim().equals("") ){
            return getDataTable(list);
        }
        int intSpecialId = Integer.parseInt(specialId);
        List<FeatherCourse> list2 = featherCourseService.selectListChooseBySpecialId(intSpecialId);
        return getDataTable(list2);
    }

    /**
     * 导出课程列表
     */
    @RequiresPermissions("cloudcourse:course:export")
    @Log(title = "课程", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(FeatherCourse featherCourse)
    {
        List<FeatherCourse> list = featherCourseService.selectFeatherCourseList(featherCourse);
        ExcelUtil<FeatherCourse> util = new ExcelUtil<FeatherCourse>(FeatherCourse.class);
        return util.exportExcel(list, "课程数据");
    }

    /**
     * 新增课程
     */
    @GetMapping("/add")
    public String add(Model model)
    {
        List<FeatherSpecial> specialList = featherSpecialService.selectFeatherSpecialList(new FeatherSpecial());
        model.addAttribute("specialList",specialList);
        return prefix + "/add";
    }

    /**
     * 新增保存课程
     */
    @RequiresPermissions("cloudcourse:course:add")
    @Log(title = "课程", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(FeatherCourse featherCourse,int[] specialIds)
    {
        return toAjax(featherCourseService.insertTwoTable(featherCourse,specialIds));
    }

    /**
     * 修改课程
     */
    @GetMapping("/edit/{courseId}")
    public String edit(@PathVariable("courseId") Long courseId, ModelMap mmap,Model model)
    {
        List<FeatherSpecial> specialList = featherSpecialService.selectFeatherSpecialList(new FeatherSpecial());
        model.addAttribute("specialList",specialList);
        FeatherCourse featherCourse = featherCourseService.selectFeatherCourseById(courseId);
        mmap.put("featherCourse", featherCourse);
        return prefix + "/edit";
    }

    /**
     * 修改保存课程
     */
    @RequiresPermissions("cloudcourse:course:edit")
    @Log(title = "课程", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(FeatherCourse featherCourse,int[] specialIds)
    {
        return toAjax(featherCourseService.updateTwoTable(featherCourse,specialIds));
    }

    /**
     * 删除课程
     */
    @RequiresPermissions("cloudcourse:course:remove")
    @Log(title = "课程", businessType = BusinessType.DELETE)
    @PostMapping( "/remove")
    @ResponseBody
    public AjaxResult remove(String ids)
    {
        return toAjax(featherCourseService.deleteTwoTableByCourseIds(ids));
    }
}
